<?php

class Salary {
        private $user;
        private $salaryInfo;
        private $salaryTotal;
        private $rates;
        private $maxHoursWeekly;
        private $adjustmentValue;
        private $adjustmentComment;
                
        public function __construct($user) {
            if($user->getUid() < 1) {
                $this->user = false;
                return;
            }
            $this->user = $user;
            $this->rates = $rates = UserDAO::getRates($this->user);
            $this->maxHoursWeekly = UserDAO::getHoursWeekly($this->user);
            $adjustmentInfo = UserDAO::getAdjustment($this->user);
            $this->adjustmentValue = $adjustmentInfo['value'];
            $this->adjustmentComment = $adjustmentInfo['comment'];
        }
        
        public function getRates() {
            return $this->rates;
        }
        
        public function getSalaryInfo() {
            return $this->salaryInfo;
        }
        
        public function getSalaryTotal() {
            return $this->salaryTotal;
        }
        
        public function getMaxHoursWeekly() {
            return $this->maxHoursWeekly;
        }
        
        public function getAdjustmentInfo() {
            return array('value'=>$this->adjustmentValue, 'comment'=>$this->adjustmentComment);
        }
        
        public function getTripsInfo($startDate, $endDate) {
            if($this->user->getUid() < 1) {
                return false;
            }
            if(!is_int($startDate)) {
                $startDate = strtotime($startDate);
            }
            if(!is_int($endDate)) {
                $endDate = strtotime($endDate);
            }
            if($startDate > $endDate) {
                $tmp = $startDate;
                $startDate = $endDate;
                $endDate = $tmp;
            }
            return WorkDAO::getTripsInfo($startDate, $endDate, $this->user);
        }
        
        
        public function calculateSalary($date) {
            if($this->user->getUid() < 1) {
                return false;
            }
            $startDate = date("Y-m", strtotime($date)) . '-01';
            $date = date('Y-m-d',strtotime($startDate . ' next month'));
            $date = date("Y-m", strtotime($date)) . '-01';
            $endDate = date('Y-m-d', strtotime($date . ' -1 day'));
            $work = new Work();
            $hours = $work->getHours($startDate, $endDate,$this->user,0);
            $normal = $work->getHours($startDate, $endDate,$this->user,1);
            $weekends = $work->getHours($startDate, $endDate,$this->user,2);
            $trips = $work->getHours($startDate, $endDate,$this->user,3);
            //0 all 1 normal(pn-pt) 2 weekends 3 trips
            if($normal > $this->maxHoursWeekly) {
                $overhours = $normal - $maxHoursWeekly;
                $normal = $maxHoursWeekly;
            } else {
                $overhours = 0;
            }
            $this->salaryInfo['all'] = $hours;
            $this->salaryInfo['normal'] = $normal;
            $this->salaryInfo['weekends'] = $weekends;
            $this->salaryInfo['overhours'] = $overhours;
            $this->salaryInfo['trips'] = $trips;
            $this->salaryTotal = 0;
            $this->salaryTotal += $normal * $this->rates->normal_rate;
            $this->salaryTotal += $weekends * $this->rates->weekend_rate;
            $this->salaryTotal += $trips * $this->rates->trip_rate;
            $this->salaryTotal += $overhours * $this->rates->overhours_rate;
            return $this->salaryTotal;
        }
}
?>
